<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="__STATIC__/lay/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="__STATIC__/lay/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <div class="layui-form layuimini-form" lay-filter="formTest1">
        <div class="layui-form-item">
            <label class="layui-form-label required">身份名称</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="身份名称不能为空" placeholder="请输入身份名称"
                    value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">权限</label>
            <div class="layui-input-block">
                <div id="test12" class="demo-tree-more"></div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="text" name="sort" placeholder="数值大的靠前" value="" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
    <script src="__STATIC__/lay/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
    <script src="__STATIC__/lay/lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script>
        layui.use(['form', 'tree', 'util'], function () {
            var form = layui.form,
                tree = layui.tree,
                layer = layui.layer,
                util = layui.util,
                $ = layui.$;
            var menuid = parent.layer.methodConfig.data.id
            //请求列表
            $.ajax({
                type: "post",
                url: "/admin/role/powerlist",
                data: {
                    id: menuid
                },
                dataType: "json",
                success: function (res) {
                    tree.render({
                        elem: '#test12',
                        data: res.data,
                        showCheckbox: true, //是否显示复选框,
                        onlyIconControl: false,
                        accordion: true,
                        id: 'demoId1',
                        isJump: false, //是否允许点击节点时弹出新窗口跳转,
                        // click: function (obj) {
                        //     var data = obj.data; //获取当前点击的节点数据
                        //     layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(
                        //         data));
                        // }
                    });
                    if (menuid != 0) {
                        $.ajax({
                            type: "post",
                            url: "/admin/role/find",
                            data: {
                                id: menuid
                            },
                            dataType: "json",
                            success: function (res) {
                               if(res.code ==1){
                                   console.log(res.data.power)
                                tree.setChecked('demoId1', res.data.power);
                                form.val('formTest1', {
                                    "name": res.data.name,
                                    "sort": res.data.sort

                                })
                                // tree.render()
                               }
                            }
                        });
                        form.render();
                    }

                }
            });


            
            //判断是否是修改
            console.log(menuid)
            //基本演示

            //监听提交
            form.on('submit(saveBtn)', function (data) {
                var checkData = tree.getChecked('demoId1');

                var list = new Array();

                list = getChecked_list(checkData);



                data.field.power = list

                if (menuid != 0) {
                    data.field.id = menuid
                    $.ajax({
                        type: "post",
                        url: "/admin/role/edit",
                        data: data.field,
                        dataType: "json",
                        success: function (response) {
                            if (response.code == 1) {
                                // layer.close(index);
                                parent.window.location.reload()
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);

                            }
                        }
                    });
                } else {
                    $.ajax({
                        type: "post",
                        url: "/admin/role/addvalue",
                        data: data.field,
                        dataType: "json",
                        success: function (response) {
                            if (response.code == 1) {
                                // layer.close(index);
                                parent.window.location.reload()
                                var iframeIndex = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(iframeIndex);

                            }
                        }
                    });
                }

                return false;
            });

            function getChecked_list(data) {
                var id = "";
                $.each(data, function (index, item) {
                    if (id != "") {
                        id = id + "," + item.id;
                    } else {
                        id = item.id;
                    }
                    var i = getChecked_list(item.children);
                    if (i != "") {
                        id = id + "," + i;
                    }
                });
                return id;
            }

        });
    </script>
</body>

</html>